import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.*;

public class NameServlet extends CommonServlet {
	private static final long serialVersionUID = 1L;

	//protected NameBean nb = new NameBean();
	
	/*protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		super.service(request, response);
		System.out.println("NameServlet.service() ");
		
		//request.setAttribute("nb", nb);
		//nb.populate(request, nb);
	}*/
	
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		super.doGet(request, response);
		System.out.println("NameServlet.doGet()");
		
		//nb.populate(request, nb);
		/*System.out.println("Previous: " + session.getAttribute("previous"));
		if(session.getAttribute("previous").equals("pizza") || session.getAttribute("previous").equals("name")) {
		} else {
			response.sendRedirect("pizza");
			System.out.println("jeeeeeeeeeeeeeeeeej");
		}*/
		boolean isValid = checkPreviousPage(response, new String[] {"pizza","name"});
		
		
		RequestDispatcher dispatcher = request.getRequestDispatcher("/Name.jsp");
		dispatcher.include(request, response);
	}
	
	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("NameServlet.doPost()");
		session.setAttribute("previous", "name");
		
		// hier doe je de dingetjes in de bean?
		nb.populate(request, nb);
	
		String name = nb.get("name");
		String address = nb.get("address");
		String city = nb.get("city");
		String phoneNumber = nb.get("phoneNumber");

		if(name == null || name.equals("")) {
			nb.put("error", "U heeft een ongeldige naam ingevuld.");
		} else if(address == null || address.equals("")) {
			nb.put("error", "U heeft een ongeldig adres ingevoerd.");
		} else if(city == null || city.equals("")) {
			nb.put("error", "U heeft een ongeldige plaats ingevoerd.");
		} else if(phoneNumber == null || phoneNumber.equals("") || phoneNumber.length() < 10) {
			nb.put("error", "U heeft een ongeldig telefoonnummer ingevoerd.");
		} else {
			nb.put("error", "");
			session.setAttribute("nb", nb);
			response.sendRedirect("money");
		}

		//Alleen bij error: nb in request & doGet
		if(nb.containsKey("error")) {
			request.setAttribute("nb", nb);
			doGet(request, response);
		}
	}
}